package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author XHui
 * @Since 2024/2/2 19:56
 * @Version 1.0
 */
@Api(value = "频道管理controller", tags = "频道管理controller")
@RestController
@RequestMapping("/api/v1/channel")
public class channelController {

    @Resource
    private ChannelService channelService;


    // 返回一个频道分页列表查询
    @ApiOperation(value = "根据条件分页查询频道信息", notes = "条件 频道名称 状态 以ord升序查询频道")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "dto", dataType = "ChannelDTO", required = true)
    )
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDTO dto, @RequestHeader("userId") String userId) {
        return channelService.findByNameAndPage(dto);
    }

    @ApiOperation("保存频道信息")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Validated(ValidatorAdd.class)
                                   AdChannel adChannel) {
        return channelService.saveChannel(adChannel);
    }

    @ApiOperation("修改频道信息")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody
                                     @Validated({ValidatorUpdate.class})
                                     AdChannel adChannel) {
        return channelService.update(adChannel);
    }

    @ApiOperation("删除频道信息")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return channelService.deleteById(id);
    }

    /**
     * 查找全部的频道信息
     *
     * @return 通用的结果返回类
     */
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }
}
